﻿using System;
using System.Windows;
using System.Windows.Controls;
using StockAnalyst2.Plugins.Engine;

namespace StockAnalyst2
{
	internal partial class MainWindow : Window, IDisposable
	{
		#region Properties

		internal new MainWindowVM DataContext
		{
			get
			{
				return (MainWindowVM)base.DataContext;
			}
			set
			{
				if (value != this.DataContext)
				{
					if (this.DataContext != null)
					{
						this.DataContext.DialogShowRequested -= DataContext_DialogShowRequested;
						this.DataContext.PluginAttachRequested -= DataContext_PluginAttachRequested; 
					}

					base.DataContext = value;

					if (this.DataContext != null)
					{
						this.DataContext.DialogShowRequested += DataContext_DialogShowRequested; // Disposed
						this.DataContext.PluginAttachRequested += DataContext_PluginAttachRequested; // Disposed
						this.DataContext.MessageBoxRequested += DataContext_MessageBoxRequested; // Disposed
					}
				}
			}
		}

		#endregion

		#region Public Methods

		public MainWindow()
		{
			InitializeComponent();
		}

		#endregion

		#region Event Handlers

		private void DataContext_MessageBoxRequested(object sender, MessageBoxRequestedEventArgs e)
		{
			MessageBox.Show(this, e.Message, e.Caption, MessageBoxButton.OK, e.Icon);
		}

		private void DataContext_DialogShowRequested(object sender, Utils.Wpf.DialogShowRequestedEventArgs e)
		{
			e.Dialog.Owner = this;
			e.DialogResult = e.Dialog.ShowDialog();
		}

		private void DataContext_PluginAttachRequested(object sender, PluginAttachRequestedEventArgs e)
		{
			this.PluginsMenu.Items.Add(e.MenuItem);
			this.MainToolBar.PluginsToolBar.Items.Add(e.ToolBarButton);
		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (this.DataContext.ApplicationCommandsVM.ExitCommand.CanExecute(null))
			{
				this.DataContext.ApplicationCommandsVM.ExitCommand.Execute(null);
			}
		}

		private void ContextMenu_Opened(object sender, RoutedEventArgs e)
		{
			((ContextMenu)this.Resources["IconMenu"]).DataContext = this.DataContext;
		}

		#endregion

		#region IDisposable

		protected bool Disposed { get; private set; }

		private void Dispose(bool disposing)
		{
			if (!this.Disposed)
			{
				this.InternalDispose(disposing);
			}

			this.Disposed = true;
		}

		protected virtual void InternalDispose(bool disposing)
		{
			if (disposing)
			{
				this.DataContext.DialogShowRequested -= DataContext_DialogShowRequested;
				this.DataContext.PluginAttachRequested -= DataContext_PluginAttachRequested;
				this.DataContext.MessageBoxRequested -= DataContext_MessageBoxRequested;
			}

			// Dispose here only unmanaged objects 
			// Don’t use managed objects here because maybe 
			// they have been finalized already
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		~MainWindow()
		{
			this.Dispose(false);
		}

		#endregion

	}
}
